﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace FTchina.Api
{
    /// <summary>
    ///Pager 的摘要说明
    /// </summary>
    public static class Pager
    {


        private static string GetrelatedPage(int CurrentPage, int PageCount, int number, string PageName)
        {
            string tempwords = string.Empty;
            int CountSmall = (CurrentPage - number > 0) ? CurrentPage - number : 1;
            int CountBig = ((CurrentPage + number) > PageCount) ? PageCount + 1 : CurrentPage + number;
            for (int i = CountSmall; i < CountBig; i++)
            {
                if (i == CurrentPage)
                {
                    tempwords += "<li class=\"p_current\">" + i + "</li>";
                }
                else
                {
                    tempwords += "<li><a href=" + PageName + "?page=" + i + " >" + i + "</a></li>";
                }
            }
            return tempwords;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Sql">获取数据的sql</param>
        /// <param name="CurrentPage">当前页</param>
        /// <param name="cid">区别标识</param>
        /// <param name="PageSize">每页数据量</param>
        /// <param name="PageName">要跳转的页面的名称</param>
        /// <returns></returns>
        public static string GetPagerWithPageName(string Sql, int AllCount, int CurrentPage, int PageSize, string PageName)
        {
            int plus1 = (AllCount % PageSize == 0) ? 0 : 1;//如果有余数，说明页数应该加1，显示不足一页的数据
            int PageCount = (AllCount / PageSize) + plus1;

            //上一
            string SyyFlag = (CurrentPage > 1) ? "<li><a href=" + PageName + "?page=" + (CurrentPage - 1) + ">上一页</a></li>" : "";
            //下一页
            string XyyFlag = (CurrentPage < PageCount) ? "<li><a href=" + PageName + "?page=" + (CurrentPage + 1) + " >下一页</a></li>" : "";

            string script = @"<script type='text/javascript' language='javascript'>" +
       " function TransPage(page) { " +
        "    var p = $(\"#txbpage\").val(); " +
         "   document.location = page + '?page=' + p;" +
        " } </script>";


            string tempwords = script + "<div class=\"pager\"><ul><li class=\"p_total\">" + CurrentPage + "/" + PageCount + "&nbsp;∑" + AllCount + "</li>" + GetrelatedPage(CurrentPage, PageCount, 5, PageName) + SyyFlag + XyyFlag + " </ul>";


            //tempwords += "<li><select class=\"p_total\" name=\"menu1\" onchange=\"javascript:window.location=(this.options[this.selectedIndex].value)\"> <option value=#>跳转</option>";

            //tempwords += MakeDDl(PageCount, PageName);
            //tempwords += "</select></li>";

            tempwords += "<input type='text' id='txbpage' style='width: 28px;' > <input type='button' class='btn'  onclick=TransPage('" + PageName + "') value='跳转' /> ";
            tempwords += "</div>";
            return tempwords;
        }


        /// <summary>
        /// 制作下拉列表
        /// </summary>
        /// <param name="PageCount"></param>
        /// <param name="PageName"></param>
        /// <returns></returns>
        private static string MakeDDl(int PageCount, string PageName)
        {

            string tempwords = string.Empty;



            for (int i = 1; i < PageCount + 1; i++)
            {
                tempwords += "<option value=\"" + PageName + "?page=" + i + "\">" + i + "</option>";
            }

            return tempwords;


        }
    }
}